package com.monitoramento.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;

import com.monitoramento.entity.Telemetria;
import com.monitoramento.util.ArduinoRequestBuilder;
import com.monitoramento.util.TelemetriaContants;

public class ArduinoServiceImpl implements ArduinoService{
	
	private HttpResponse sendParamRequest(String url){		
		
		DefaultHttpClient httpclient = new DefaultHttpClient();
		
		HttpGet httpget = new HttpGet(url);
		
		try {
		
			return httpclient.execute(httpget);
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	@Override
	public Telemetria getValoresSensores() {
		
		try {
		
			Telemetria telemetria = new Telemetria();
			DefaultHttpClient httpclient = new DefaultHttpClient();
			
			HttpGet httpget = new HttpGet("TelemetriaContants.ARDUINO_URL");
	        HttpResponse response = null;
	        
			response = httpclient.execute(httpget);
			
	        HttpEntity entity = response.getEntity();
	        
	        if (entity != null) {
	        	
	            InputStream instream = entity.getContent();
	
	            JSONObject myAwway = new JSONObject(convertStreamToString(instream));
	
	            // Get the query value'
	           telemetria.setSensor1(myAwway.getString("sensor_1"));           
	           telemetria.setSensor2(myAwway.getString("sensor_2"));
	           telemetria.setSensor3(myAwway.getString("sensor_3"));
	           telemetria.setSensor4(myAwway.getString("sensor_4"));
	        
	           instream.close();
	        
	        }
	        
	        return telemetria;
        
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }


	@Override
	public void iniciaCronometro() {
		
		ArduinoRequestBuilder builder = new ArduinoRequestBuilder();
		
		sendParamRequest(builder.requestIniciaCronometro());
		
	}
	
	public void getTempoTotalDeProva() {
		
		try{
		
			ArduinoRequestBuilder builder = new ArduinoRequestBuilder();
			
			HttpResponse httpResponse = sendParamRequest(builder.requestGetTempoTotalDeProva());
			
			 HttpEntity entity = httpResponse.getEntity();
		        
	        if (entity != null) {
	        	
	            InputStream instream = entity.getContent();
	
	            JSONObject myAwway = new JSONObject(convertStreamToString(instream));
	
	            // Get the query value
	           myAwway.getString(TelemetriaContants.ARDUINO_JSON_TEMPO_TOTAL);
	           
	           instream.close();
        
        }
	        
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}		
		
	}
	
}
